// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-11-19

package log

import (
	"context"
	"gitee.com/go-libs/log/base"
	"gitee.com/go-libs/log/common"
	"gitee.com/go-libs/log/conf"
	"gitee.com/go-libs/log/logger"
	"gitee.com/go-libs/log/tracer"
)

type (
	Level  = base.Level
	Tracer = base.Tracer
)

type (
	Span    = common.Span
	Tracing = common.Tracing
)

// +---------------------------------------------------------------------------+
// | Alias for context                                                         |
// +---------------------------------------------------------------------------+

var (
	AttachRequest        = common.AttachRequest
	ContextHeader        = common.GetContextHeader
	ContextTracing       = common.GetContextTracing
	Context              = common.NewContext
	NewContext           = common.NewContext
	NewContextWithHeader = common.NewContextWithHeader
)

// NewContextForChildInfo
// add Infofc log before create child log context. It's worked like follow.
//
//	log.Infofc(parent, "info message")
//	ctx := log.NewContext(parent)
func NewContextForChildInfo(parent context.Context, format string, args ...any) context.Context {
	Infofc(parent, format, args...)
	return NewContext(parent)
}

// +---------------------------------------------------------------------------+
// | Alias for span                                                            |
// +---------------------------------------------------------------------------+

var (
	SpanManager = tracer.New
)

var (
	NewSpan            = tracer.NewSpan
	NewSpanWithContext = tracer.NewSpanWithContext
	NewSpanWithHeader  = tracer.NewSpanWithHeader
	NewSpanWithRequest = tracer.NewSpanWithRequest
)

// +---------------------------------------------------------------------------+
// | Alias for tracing                                                         |
// +---------------------------------------------------------------------------+

var (
	NewTracing            = common.NewTracing
	NewTracingWithContext = common.NewTracingWithContext
	NewTracingWithHeader  = common.NewTracingWithHeader
	NewTracingWithRequest = common.NewTracingWithRequest
)

// +---------------------------------------------------------------------------+
// | Alias for logging                                                         |
// +---------------------------------------------------------------------------+

var (
	LoggingManager = logger.New
)

var (
	Debug   = logger.Debug
	Debugf  = logger.Debugf
	Debugfc = logger.Debugfc
	Error   = logger.Error
	Errorf  = logger.Errorf
	Errorfc = logger.Errorfc
	Fatal   = logger.Fatal
	Fatalf  = logger.Fatalf
	Fatalfc = logger.Fatalfc
	Info    = logger.Info
	Infof   = logger.Infof
	Infofc  = logger.Infofc
	Panic   = logger.Fatal
	Panicf  = logger.Fatalf
	Panicfc = logger.Fatalfc
	Warn    = logger.Warn
	Warnf   = logger.Warnf
	Warnfc  = logger.Warnfc
)

// +---------------------------------------------------------------------------+
// | Alias for common                                                          |
// +---------------------------------------------------------------------------+

var (
	Config = conf.New
)

// Close
// 关闭日志, 此过程阻塞协程, 直到所有链路跟踪与日志上报完成.
func Close() {
	SpanManager().Wait()
	LoggingManager().Wait()
}
